#include "people.h"
#include <fstream>
#include "boolexp.h"

People::People()
{
}

People::~People(){

    _peopleMap.clear();
}

void People::deletePeople(){
    QMap<int, Person*>::iterator i;
    for (i = _peopleMap.begin(); i != _peopleMap.end(); ++i){
        SAFE_DELETE(i.value());
    }
}

void People::Add(Person *p){
    _peopleMap.insert(p->getId(), p);
}

void People::testAndPrint(QString str){
    QMap<int, Person*>::iterator i;
    for (i = _peopleMap.begin(); i != _peopleMap.end(); ++i){
        if(CompositeExp::testCondition(str, i.value())){
            Person *pP = i.value();
            if(pP->getGender() == MALE){
                Man *pM = static_cast<Man *>(pP);
                cout << *pM;
            }else{
                Woman *pW = static_cast<Woman *>(pP);
                cout << *pW;
            }
        }
    }
}

bool People::Remove(int id){
    if(_peopleMap.remove(id) >0 )
        return true;
    else
        return false;
}

void People::output2Console(){
    QMap<int, Person*>::iterator i;
    for (i = _peopleMap.begin(); i != _peopleMap.end(); ++i){
        i.value()->outPut2Console();
    }
}


void People::save(QString filename, Gender gender){
    ofstream os;
    try{
        os.open(filename.toAscii());
    }catch(int){
        qDebug("Error in people::save!\n");
        return;
    }
    int count = _peopleMap.count();
    os << "count:" << count << endl;
    os << "gender:" << Person::GenderStr[gender].toStdString() << endl;
    QMap<int, Person*>::iterator i;
    for (i = _peopleMap.begin(); i != _peopleMap.end(); ++i){
        Person *pP = i.value();
        if(pP->getGender() == MALE){
            Man *pM = static_cast<Man *>(pP);
            os << *pM;
        }else{
            Woman *pW = static_cast<Woman *>(pP);
            os << *pW;
        }
    }

    os.close();
}
void People::load(QString filename){
    ifstream is(filename.toAscii());
    if(!is){
        qDebug("Error in people::load!\n");
        return;
    }

    int count = 0;
    count = Person::getInfoFromIstream(is).toInt();

    if(Person::GenderStr[MALE] == Person::getInfoFromIstream(is)){
     for(int i = 0 ; i < count ; i++){
         Man *pMan = new Man();
         is >> *pMan;
         Add(pMan);
      }
    }else{
        for(int i = 0 ; i < count ; i++){
            Woman *pWoman = new Woman();
            is >> *pWoman;
            Add(pWoman);
         }
    }

    is.close();
}

Person *People::getPerson(int id){
    return _peopleMap.value(id);
}

QStringList People::getWomanConditionStrList(){
    QStringList sl;
    QMap<int, Person*>::iterator i;
    for (i = _peopleMap.begin(); i != _peopleMap.end(); ++i){
        Person *pP = i.value();
        if(pP->getGender() == MALE){

        }else{
            Woman *pW = static_cast<Woman *>(pP);
            sl.append(pW->getConditionStr());
        }
    }
    return sl;
}
